//
//  LeaveMessageViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/15.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "LeaveMessageViewController.h"
#import "UIPlaceHolderTextView.h"
@interface LeaveMessageViewController ()
@property (nonatomic, strong)UIPlaceHolderTextView * textView;
@property (nonatomic, strong)NSString * openIDString;//微信id
@property (nonatomic, strong)NSString * mobileString;//电话号码
@end

@implementation LeaveMessageViewController(networking)
- (void)leaveMessageData{
    NSDictionary * dic = @{
                           @"openid":self.openIDString,
                           @"mobile":self.mobileString,
                           @"messageinfo":self.textView.text
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"user/leaMessage" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            ShowMessage(responseObject[@"msg"]);
            int64_t delayInSeconds = 1.0;      // 延迟的时间
            /*
             *@parameter 1,时间参照，从此刻开始计时
             *@parameter 2,延时多久，此处为秒级，还有纳秒等。10ull * NSEC_PER_MSEC
             */
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                // do something
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation LeaveMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"写留言";
    //右侧消息按钮
    UIButton * naviRightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40/WIDTH_6_SCALE, 20/WIDTH_6_SCALE)];
    [naviRightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [naviRightBtn setTitle:@"提交" forState:UIControlStateNormal];
    [naviRightBtn addTarget:self action:@selector(naviRightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    naviRightBtn.titleLabel.font = DEF_FontSize_12;
    UIBarButtonItem * rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviRightBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    //输入内容
    self.textView = [[UIPlaceHolderTextView alloc]initWithFrame:CGRectMake(0, 13/WIDTH_6_SCALE, SCREEN_WIDTH - 26/WIDTH_6_SCALE, 154/WIDTH_6_SCALE)];
    self.textView.backgroundColor = [UIColor whiteColor];
    self.textView.placeholder = @"请输入您的留言，留言将由博帮互助筛选后显示，对所有人可见";
    [self.view addSubview:self.textView];
}
#pragma mark - action
- (void)naviRightBtnClick:(UIButton *)sender{
    NSLog(@"提交");
    if ([UserModel sharedInstanced].openId == nil && [UserModel sharedInstanced].mobile == nil) {
        ShowMessage(@"请先登录");
        return;
    }
    if ([UserModel sharedInstanced].openId == nil) {
        self.openIDString = @"";
    }
    if ([UserModel sharedInstanced].mobile == nil) {
        self.mobileString = @"";
    }
    [self leaveMessageData];
}
@end
